<?php

/**
 * @user magein
 * @date 2023/12/28 17:38
 */

namespace magein\utils;

class BrowserAgent
{

    /**
     * 谷歌浏览器
     * @return string
     */
    public static function chrome(): string
    {
        $version = sprintf('%s.%s.%s.%s', rand(90, 999), rand(0, 1), rand(1000, 9999), rand(100, 999));

        return sprintf('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36', $version);
    }

    /**
     * 火狐浏览器
     * @return string
     */
    public static function firefox(): string
    {
        $version = sprintf('%s.%s', rand(10, 99), rand(0, 99));

        return sprintf('Mozilla/5.0 (Windows NT 5.1; rv:%s) Gecko/20100101 Firefox/%s', $version, $version);
    }

    /**
     * 欧朋浏览器
     * @return string
     */
    public static function opera(): string
    {
        $version1 = sprintf('%s.%s', rand(5, 10), rand(0, 9));
        $version2 = sprintf('%s.%s', rand(12, 99), rand(0, 20));

        return sprintf('Opera/9.80 (Windows NT %s; Win64; x64) Presto/2.12.388 Version/%s', $version1, $version2);
    }

    /**
     * safari浏览器
     * @return string
     */
    public static function safari(): string
    {
        $version1 = sprintf('%s.%s', rand(7, 12), rand(0, 9));
        $version2 = sprintf('%s.%s.%s.%s', rand(60, 120), rand(0, 20), rand(1000, 9999), rand(100, 999));
        $version3 = sprintf('%s.%s', rand(537, 750), rand(10, 99));

        return sprintf('Mozilla/5.0 (Windows NT %s; Win64; x64; WebView/3.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/%s', $version1, $version2, $version3);
    }


    /**
     * @return string
     */
    public static function random(): string
    {
        $rand = rand(0, 4);
        $self = new self();
        switch ($rand) {
            case 1:
                $agent = $self->safari();
                break;
            case 2:
                $agent = $self->opera();
                break;
            case 3:
                $agent = $self->firefox();
                break;
            case 4:
                $agent = $self->android();
                break;
            default:
                $agent = $self->chrome();
                break;
        }
        return $agent;
    }

    /**
     * @return string[]
     */
    public static function header(): array
    {
        return [
            'User-Agent: ' . self::random(),
        ];
    }

    /**
     * @return string
     */
    public static function android(): string
    {
        $lists = [
            'Mozilla/5.0 (Linux; Android 10.1.0; Galaxy S10 Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/64.0.1025.133 Mobile Safari/535.19',
            'Mozilla/5.0 (Linux; Android 10.0.0; Galaxy Nexus Build/IMM76B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3584.76 Mobile Safari/537.36',
            'Mozilla/5.0 (Linux; Android 9.0; SM-G965F Build/LMY47V) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36',
            'Mozilla/5.0 (Linux; Android 8.1.0; ONEPLUS A5010 Build/OPM1.171019.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.86 Mobile Safari/537.36 EdgA/42.0.0.2801',
            'Mozilla/5.0 (Linux; Android 7.0; LGL84VL Build/NRD90U; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 Mobile Safari/537.36',
            'Mozilla/5.0 (Linux; Android 10.0.0; Galaxy Nexus Build/IMM76B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3584.76 Mobile Safari/537.36',
            'Mozilla/5.0 (Linux; Android 8.1.0; ONEPLUS A5010 Build/OPM1.171019.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.86 Mobile Safari/537.36 EdgA/42.0.0.2801',
            'Mozilla/5.0 (Linux; Android 9.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36',
            'Opera/9.80 (Android; Opera Mini/38.0.2254/121.103; U; en) Presto/2.12.423 Version/12.16',
            'Opera/9.80 (Android; Opera Mini/27.0.2254/65.257; U; hu) Presto/2.12.423 Version/12.16',
            'Opera/9.80 (Android; Opera Mini/22.0.1485/35.3730; U; es) Presto/2.8.119 Version/11.10',
        ];

        return $lists[rand(0, count($lists) - 1)];
    }


}